/**
 *    Copyright (C) 2008  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _BM_TOOLS_H
#define	_BM_TOOLS_H

#include <stdlib.h>
#include <iostream>

namespace cubefs {
namespace core {

long int get_random(size_t max);
long int get_random0(size_t max);
long int getTime();
int get_number(char *st);
int test_pow2(int value);

template <typename Task>
class benchmark {
    Task task;
public:
    benchmark(Task &t): task(t) {}

    float run(int times) {
        task.prepare();

        long int t0 = getTime();
        for (int c = 0; c < times; c++) {
            task.calibrate();
        }
        long int t1 = getTime();

        for (int c = 0; c < times; c++) {
            task.run();
        }

        long int t2 = getTime();

        //std::cout<<"times: "<<(t2-t1)<<" "<<(t1-t0)<<std::endl;

        return ((t2 - t1) - (t1 - t0))/(float)times;
    }
};

} //core
} //cubefs

#endif	/* _BM_TOOLS_H */

